package com.alisls.shequtao.service.goods.service.impl;

import com.alisls.shequtao.common.model.query.PageQuery;
import com.alisls.shequtao.common.model.result.PageResult;
import com.alisls.shequtao.service.goods.dao.BrandDAO;
import com.alisls.shequtao.service.goods.dto.BrandDTO;
import com.alisls.shequtao.service.goods.dto.CategoryDTO;
import com.alisls.shequtao.service.goods.entity.BrandDO;
import com.alisls.shequtao.service.goods.entity.CategoryBrandDO;
import com.alisls.shequtao.service.goods.query.BrandPageQuery;
import com.alisls.shequtao.service.goods.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 品牌服务
 *
 * @author Ke Wang
 */
@Service("brandService")
@AllArgsConstructor
@Slf4j
public class BrandServiceImpl extends ServiceImpl<BrandDAO, BrandDO> implements BrandService {

    private final BrandDAO brandDAO;

    @Override
    public PageResult<BrandDTO> listBrandByPage(PageQuery pageQuery) {
        Page<BrandDO> page = new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize());
        QueryWrapper<BrandDO> queryWrapper = new QueryWrapper<>();

        // 模糊查询关键字
        String key = ((BrandPageQuery) pageQuery).getKey();
        if (key != null) {
            queryWrapper.like("brand_name", key.trim());
        }

        // 排序字段
        String orderField = pageQuery.getOrderField();
        if (!StringUtils.isEmpty(orderField)) {
            if (pageQuery.getIsAsc()) {
                queryWrapper.orderByAsc(orderField);
            } else {
                queryWrapper.orderByDesc(orderField);
            }
        }

        // 进行分页查询
        IPage<BrandDO> brandDOPage =  brandDAO.selectPage(page, queryWrapper);
        return convertToPageResult(brandDOPage);
    }

    @Override
    public List<BrandDTO> listBrandsByCategoryId(Long categoryId) {
        List<BrandDO> brandDOs = brandDAO.getBrandsByCategoryId(categoryId);
        if (CollectionUtils.isEmpty(brandDOs)) {
            return null;
        }
        return brandDOs.stream().map(this::convertToDTO).collect(Collectors.toList());
    }

    /**
     * 保存品牌
     * @param brandDTO 品牌对象
     * @return 品牌对象
     */
    @Override
    public BrandDTO save(@Nullable BrandDTO brandDTO) {
        Assert.notNull(brandDTO, "保存品牌对象时数据为空!");

        BrandDO brandDO = new BrandDO();
        BeanUtils.copyProperties(brandDTO, brandDO);

        this.save(brandDO);
        return brandDTO;
    }

    /**
     * 保存品牌（品牌带有商品分类）
     * @param categoryDTO 分类
     * @param brandDTO 品牌
     * @return 品牌对象
     */
    @Override
    public BrandDTO saveCategoryAndBrand(CategoryDTO categoryDTO, BrandDTO brandDTO) {
        BrandDO brandDO = new BrandDO();
        BeanUtils.copyProperties(brandDTO, brandDO);
        brandDAO.insert(brandDO);

        CategoryBrandDO categoryBrandDO = new CategoryBrandDO();
        categoryBrandDO.setCategoryId(categoryDTO.getId());
        categoryBrandDO.setBrandId(brandDO.getId());

        brandDAO.saveCategoryAndBrand(categoryBrandDO);
        return brandDTO;
    }

    @Override
    public BrandDTO convertToDTO(BrandDO brandDO) {
        if (brandDO == null) {
            return null;
        }

        BrandDTO brandDTO = new BrandDTO();
        BeanUtils.copyProperties(brandDO, brandDTO);
        return brandDTO;
    }

    public List<BrandDTO> convertToDTOs(List<BrandDO> brandDOs) {
        return brandDOs.stream().map(brandDO -> {
            BrandDTO brandDTO = new BrandDTO();
            BeanUtils.copyProperties(brandDO, brandDTO);
            return brandDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public BrandDO convertToDO(BrandDTO brandDTO) {
        if (brandDTO == null) {
            return null;
        }

        BrandDO brandDO = new BrandDO();
        BeanUtils.copyProperties(brandDTO, brandDO);
        return brandDO;
    }

    public PageResult<BrandDTO> convertToPageResult(IPage<BrandDO> pageData) {
        PageResult<BrandDTO> pageResult = new PageResult<>();
        List<BrandDTO> specGroupDTOs = convertToDTOs(pageData.getRecords());
        pageResult.setRows(specGroupDTOs);
        pageResult.setCurrentPage(pageData.getCurrent());
        pageResult.setPageSize(pageData.getSize());
        pageResult.setTotalPages(pageData.getPages());
        pageResult.setTotalCount(pageData.getTotal());
        return pageResult;
    }
}
